package com.augurit.common.system.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.extmodel.base.BaseTreeNode;
import com.augurit.common.base.extmodel.base.HtmlElement;
import com.augurit.common.base.extmodel.inner.MenuLinkTreeNode;
import com.augurit.common.base.extmodel.inner.MultiEntityTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.AcApplication;
import com.augurit.common.entity.AcFunction;
import com.augurit.common.entity.AcMenu;
import com.augurit.common.entity.AcServer;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.convert.AcServerConverter;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.system.web.org.form.AcApplicationForm;
import com.augurit.common.system.web.org.form.AcFunctionForm;
import com.augurit.common.system.web.org.form.AcMenuForm;
import com.augurit.common.system.web.org.form.AcModuleForm;
import com.augurit.common.system.web.org.form.AcRoleForm;
import com.augurit.common.system.web.org.form.AcServerForm;
import com.augurit.common.system.web.org.form.AtHomepageConfigForm;
import com.augurit.common.system.web.org.form.OmGroupForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.CollectionUtils;

public class OrgUtils {

	public static void setAcServerInfoToApp(AcApplicationForm form, List<AcServerForm> acServerFormList){
		if(form != null)
			setAcServerInfoToApp(form, findAcServerForm(acServerFormList, form.getServId()));
	}
	
	public static void setAcServerInfoToApp(AcApplicationForm form, AcServerForm acServerForm){
		if(form != null && acServerForm != null){
			form.setServName(acServerForm.getServName());
			form.setServIp(acServerForm.getServIp());
			form.setServPort(acServerForm.getServPort());
//			form.setAppUrl(calcAppUrl(acServerForm, form.getAppCode()));
		}
	}
	
	public static void setAcServerInfoToApp(AcApplicationForm form, AcServer acServer){
		setAcServerInfoToApp(form, AcServerConverter.convertToForm(acServer));
	}

	public static AcServerForm findAcServerForm(List<AcServerForm> acServerFormList, Long servId){
		if(acServerFormList != null && acServerFormList.size() > 0 && servId != null){
			for(AcServerForm form : acServerFormList){
				if(form.getServId() != null && form.getServId().equals(servId))
					return form;
			}
		}
		return null;
	}
	
	public static String calcAppUrl(AcServerForm acServerForm, String appCode){
		return calcAcServerUrl(acServerForm) + "/" + appCode;
	}
	
	public static String calcAcServerUrl(AcServerForm acServer){
		String result = "";
		if(acServer != null){
			result = "http://" + acServer.getServIp() + ":" + acServer.getServPort();
		}
		return result;
	}
	
	public static String convertAcServerInfoToString(AcServerForm acServer){
		String result = calcAcServerUrl(acServer);
		if(acServer.getServName() != null && acServer.getServName().trim().length() > 0){
			result = connectStr(acServer.getServName(), result);
		}
		return result;
	}
	
	public static String connectStr(String str1, String str2){
		return (str1 == null ? "" : str1) + " ( " + (str2 == null ? "" : str2) + " )";
	}
	
	/**
	 * 将含子菜单的菜单对象列表转换为Ext折叠面板内容
	 * @param list 含子菜单的菜单对象列表
	 * @return
	 */
	public static List<HtmlElement> convertToHtmlElementForAccordions(List<AcMenuForm> list){
		List<HtmlElement> result = new ArrayList();
		
		if(list != null && list.size() > 0){
			for(AcMenuForm form : list){
				HtmlElement element = new HtmlElement();
				element.setId(form.getMenuId().toString());
				element.setText(form.getMenuName());
				element.setIcon(form.getSmallImgPath());
				element.setHtml(convertChildrenToAccordionHtml(form.getChildren()));
				result.add(element);
			}
		}
		
		return result;
	}
	
	/**
	 * 将子菜单列表转换为Ext折叠面板要显示的HTML页面内容
	 * @param children 子菜单列表
	 * @return
	 */
	private static String convertChildrenToAccordionHtml(List<AcMenuForm> children){
		String result = "<div align=\"center\">";
		
		if(children != null && children.size() > 0){
			for(AcMenuForm form : children){
				result += "<div class=\"link\"><a href=\"javascript:executeFuncUrl('" + ((form.getMenuUrl() == null || form.getMenuUrl().trim().length() == 0) ? "#" : form.getMenuUrl()) + "')\">"
						+ "<img src=\"" + ((form.getBigImgPath() == null || form.getBigImgPath().trim().length() == 0) ? "#" : form.getBigImgPath()) + "\"/><div class=\"link_text\">" + form.getMenuName()
						+ "</div></a></div>";
			}
		}
		
		result += "</div>";
		
		return result;
	}
	
	/**
	 * 当没有设置启用首页时，由程序指定默认首页设置
	 * @param atHomepageConfigForm
	 * @return
	 */
	public static AtHomepageConfigForm getValidHomepageConfig(AtHomepageConfigForm atHomepageConfigForm){
		if(atHomepageConfigForm == null){
			atHomepageConfigForm = new AtHomepageConfigForm();
			atHomepageConfigForm.setHomepageCode(OrgConstant.HOMEPAGE_CODE_DEFAULT);
			atHomepageConfigForm.setHomepageName(OrgConstant.HOMEPAGE_NAME_DEFAULT);
			atHomepageConfigForm.setLoadMenuLevel(0);
			atHomepageConfigForm.setLoadByTree(OrgConstant.ZERO);
			atHomepageConfigForm.setModelName(OrgConstant.HOMEPAGE_MODEL_NAME_DEFAULT);
		}
		return atHomepageConfigForm;
	}
	
	/**
	 * 根据登录用户获取可访问的菜单ID集合
	 * @param loginUser
	 * @return
	 */
	public static String getPrivilegeWhereClause(String acMenuEntityAlias, LoginUserForm loginUser){
		String result = "";
		
		if(loginUser != null){
			if(loginUser.getMenuIds() != null && loginUser.getMenuIds().length > 0){
				result = " and " + HqlUtils.buildOrCondition(acMenuEntityAlias, "menuId", loginUser.getMenuIds());
			}else{
				result = " and 1!=1";
			}
		}
		
		return result;
	}
	
	/**
	 * 根据指定要加载的菜单层次区间构造where条件查询分句.
	 * 注意：如果不指定父菜单的菜单层次，则parentMenuLevel设定为-1，则从第一层菜单开始加载
	 * 例如：当parentMenuLevel=2且depth=3，则分句为：and (ps.menuLevel=3 or ps.menuLevel=4 or ps.menuLevel=5)
	 * @param entityAlias entity对象别名
	 * @param parentMenuLevel 父菜单的菜单层次
	 * @param depth 要加载的菜单层次深度
	 * @return
	 */
	public static String getMenuDepthWhereClause(String entityAlias, int parentMenuLevel, int depth){
		String result = "";
		
		// 定义要加载的菜单层次区间
		int startLoadMenuLevel = (parentMenuLevel == -1) ? 1 : (parentMenuLevel + 1);
		int endLoadMenuLevel = (parentMenuLevel == -1) ? depth : (parentMenuLevel + depth);
		
		if(startLoadMenuLevel > 0 && endLoadMenuLevel >= startLoadMenuLevel){
			
			result += " and (";
			
			for(int i = startLoadMenuLevel; i <= endLoadMenuLevel; i++){
				result += entityAlias + ".menuLevel=" + i;
				
				if(i != endLoadMenuLevel)
					result += " or ";
			}
			
			result += ") ";
		}
		
		return result;
	}
	
	/**
	 * 根据指定要加载的菜单层次构造where条件查询分句
	 * 例如：and (ps.menuLevel=1 or ps.menuLevel=2)
	 * @param entityAlias entity对象别名
	 * @param menuLevel	要加载的菜单层次
	 * @return
	 */
	public static String getMenuLevelWhereClause(String entityAlias, int loadMenuLevel){
		String result = "";
		
		int depth = loadMenuLevel;
		
		if(depth > 0){
			
			if(depth > OrgConstant.INDEX_MAX_LOAD_MENU_LEVEL)
				depth = OrgConstant.INDEX_MAX_LOAD_MENU_LEVEL;
			
			result = getMenuDepthWhereClause(entityAlias, -1, depth);
		}
		
		return result;
	}
	
	/**
	 * 根据父菜单ID构造查询本身菜单及所有子菜单对象的where条件查询分句
	 * 例如：and (ps.menuSeq like '%.1.%')
	 * @param entityAlias entity对象别名
	 * @param parentMenuId 父菜单ID
	 * @param includeParentMenu 是否包含父菜单本身
	 * @return
	 */
	public static String getParentMenuAndChildrenWhereClause(String entityAlias, Long parentMenuId, boolean includeParentMenu){
		String result = "";
		
		if(parentMenuId != null && parentMenuId.intValue() >= 0){
			if(includeParentMenu)
				result += " and " + parentMenuAndChildrenLikeStr(entityAlias, "menuSeq", parentMenuId);
			else
				result += " and " + childrenLikeStr(entityAlias, "menuSeq", parentMenuId);
		}
		
		return result;
	}
	
	/**
	 * 构造查询子菜单的LIKE字符串
	 * @param parentMenuId 父菜单ID
	 * @return
	 */
	private static String childrenLikeStr(String entityAlias, String menuSeqProperty, Long parentMenuId){
		if(parentMenuId != null)
			return "(" + entityAlias + "." + menuSeqProperty + " like '%." + parentMenuId + ".%' and " + entityAlias + "." + menuSeqProperty + " not like '%." + parentMenuId + ".')";
		else
			return entityAlias + "." + menuSeqProperty + " like '%'";
	}
	
	/**
	 * 构造查询本菜单及子菜单的LIKE字符串
	 * @param parentMenuId 父菜单ID
	 * @return
	 */
	private static String parentMenuAndChildrenLikeStr(String entityAlias, String menuSeqProperty, Long parentMenuId){
		if(parentMenuId != null)
			return entityAlias + "." + menuSeqProperty + " like '%." + parentMenuId + ".%' ";
		else
			return entityAlias + "." + menuSeqProperty + " like '%'";
	}
	
	/**
	 * 获取AcMenuForm对象列表中菜单层次最小值
	 * @param list AcMenuForm对象列表
	 * @return
	 */
	public static int getMinMenuLevel(List<AcMenuForm> list){
		int result = 99;
		
		if(list != null && list.size() > 0){
			for(AcMenuForm form : list){
				if(form.getMenuLevel().intValue() < result)
					result = form.getMenuLevel().intValue();
			}
		}
		
		return result;
	}
	
	/**
	 * 判断AcMenuForm对象是否在指定列表中存在
	 * @param form AcMenuForm对象
	 * @param list 要比较的AcMenuForm对象列表
	 * @return
	 */
	public static boolean exist(AcMenuForm form, List<AcMenuForm> list){
		if(list != null && list.size() > 0){
			for(AcMenuForm temp : list){
				if(temp.getMenuId().longValue() == form.getMenuId().longValue())
					return true;
			}
		}
		return false;
	}
	
	/**
	 * 获取OmOrgForm对象列表中菜单层次最小值
	 * @param list OmOrgForm对象列表
	 * @return
	 */
	public static int getMinOrgLevel(List<OmOrgForm> list){
		int result = 99;
		
		if(list != null && list.size() > 0){
			for(OmOrgForm form : list){
				if(form.getOrgLevel().intValue() < result)
					result = form.getOrgLevel().intValue();
			}
		}
		
		return result;
	}
	
	/**
	 * 获取OmPositionForm对象列表中菜单层次最小值
	 * @param list OmPositionForm对象列表
	 * @return
	 */
	public static int getMinPosLevel(List<OmPositionForm> list){
		int result = 99;
		
		if(list != null && list.size() > 0){
			for(OmPositionForm form : list){
				if(form.getPosLevel().intValue() < result)
					result = form.getPosLevel().intValue();
			}
		}
		
		return result;
	}
	
	/**
	 * 获取OmModuleForm对象列表中菜单层次最小值
	 * @param list OmModuleForm对象列表
	 * @return
	 */
	public static int getMinModuleLevel(List<AcModuleForm> list){
		int result = 99;
		
		if(list != null && list.size() > 0){
			for(AcModuleForm form : list){
				if(form.getModuleLevel().intValue() < result)
					result = form.getModuleLevel().intValue();
			}
		}
		
		return result;
	}
	
	
	/**
	 * 判断OmOrgForm对象是否在指定列表中存在
	 * @param node OmOrgForm对象
	 * @param list 要比较的OmOrgForm对象列表
	 * @return
	 */
	public static boolean exist(OmOrgForm node, List<OmOrgForm> list){
		if(list != null && list.size() > 0){
			for(OmOrgForm temp : list){
				if(temp.getOrgId().equals(node.getOrgId()))
					return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断OmPositionForm对象是否在指定列表中存在
	 * @param node OmPositionForm对象
	 * @param list 要比较的OmPositionForm对象列表
	 * @return
	 */
	public static boolean exist(OmPositionForm node, List<OmPositionForm> list){
		if(list != null && list.size() > 0){
			for(OmPositionForm temp : list){
				if(temp.getPosId().equals(node.getPosId()))
					return true;
			}
		}
		return false;
	}
	
	
	public static boolean exist(AcModuleForm node, List<AcModuleForm> list){
		if(list != null && list.size() > 0){
			for(AcModuleForm temp : list){
				if(temp.getModuleId().equals(node.getModuleId()))
					return true;
			}
		}
		return false;
	}
	
	
	/**
	 * form是否存在与list中
	 * @param form
	 * @param list
	 * @return
	 */
	public static boolean exist(AcRoleForm form, List<AcRoleForm> list){
		if(form != null && list != null && list.size() > 0){
			for(AcRoleForm temp : list){
				if(temp.getRoleId() == form.getRoleId()){
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 合并两个角色列表
	 * @param list1
	 * @param list2
	 */
	public static void mergeRoleList(List<AcRoleForm> list1, List<AcRoleForm> list2){
		if(list1 != null && list1.size() > 0 && list2 != null && list2.size() > 0){
			for(AcRoleForm form : list2){
				if(!OrgUtils.exist(form, list1)){
					list1.add(form);
				}
			}
		}
	}
	
	/**
	 * 将序列拆分为字符串数组
	 * 注意：序列以“.”作为分隔，如：“.4.20.”
	 * @param seq
	 * @return
	 */
	public static String[] splitSeqToArray(String seq){
		if(seq != null && seq.trim().length() > 0){
			return seq.substring(1).split("\\.");
		}else
			return null;
	}
	
	
	
	//解析字符串，然后转换成数组
	public static Long[] splitString(String idStr){
		Long [] ids = null;
		if(idStr != null){
			String [] str_ids = idStr.split(",");
			if(str_ids != null && (str_ids.length) !=0){
				ids = new Long[(str_ids.length)];
				for (int i = 0; i < str_ids.length; i++) 
					ids[i] = Long.parseLong(str_ids[i]);
			}
		}
		return  ids;
	}
	
	public static boolean isAppUrlChange(AcApplicationForm form, AcApplication entity){
		boolean result = false;
		
		if(form != null && entity != null){
			if((form.getAppUrl() == null || form.getAppUrl().trim().length() == 0)
					&& (entity.getAppUrl() != null && entity.getAppUrl().trim().length() > 0)){
				result = true;
			}else if((entity.getAppUrl() == null || entity.getAppUrl().trim().length() == 0)
					&& (form.getAppUrl() != null && form.getAppUrl().trim().length() > 0)){
				result = true;
			}else if(form.getAppUrl() != null && entity.getAppUrl() != null 
					&& !form.getAppUrl().equals(entity.getAppUrl())){
				result = true;
			}
		}
		
		return result;
	}
	
	/**
	 * 检查功能调用入口是否合法
	 * @param form
	 */
	public static void checkFuncUrlFormat(AcFunctionForm form){
		if(form != null && form.getFuncUrl() != null){
			//去除多余空格
			form.getFuncUrl().trim();
			
			//检查格式
			if(form.getUrlType().equals(OrgConstant.ZERO)
					&& !form.getFuncUrl().startsWith("/"))
				form.setFuncUrl("/" + form.getFuncUrl());
			
			else if(form.getUrlType().equals(OrgConstant.ONE)
					&& !form.getFuncUrl().startsWith("http://"))
				form.setFuncUrl("http://" + form.getFuncUrl());
			
			else if(form.getUrlType().equals(OrgConstant.TWO)
					&& !form.getFuncUrl().startsWith("javascript:"))
				form.setFuncUrl("javascript:" + form.getFuncUrl());
		}
	}
	
	/**
	 * 判断两个对象的funcUrl属性值是否相同
	 * @param form
	 * @param entity
	 * @return true表示funcUrl属性值不相同，false表示相同
	 */
	public static boolean isFuncUrlDifference(AcFunctionForm form, AcFunction entity){
		if(form != null && entity != null && form.getFuncUrl() != null && entity.getFuncUrl() != null 
					&& !form.getFuncUrl().equals(entity.getFuncUrl()))
			return true;
		else
			return false;
	}
	
	public static boolean isIpOrPortChange(AcServerForm form, AcServer entity){
		boolean result = false;
		
		if(form != null && entity != null){
			if(!form.getServIp().equals(entity.getServIp()))
				result = true;
			else if(!form.getServPort().equals(entity.getServPort()))
				result = true;
		}
		
		return result;
	}
	
	public static Map<Long, AcApplicationForm> converToAppMap(List<AcApplicationForm> acApplicationList, List<AcServerForm> acServerList){
		if(acApplicationList != null && acApplicationList.size() > 0){
			Map<Long, AcApplicationForm> acApplicationMap = new HashMap();
			
			for(AcApplicationForm form : acApplicationList){
				AcServerForm acServer = OrgUtils.findAcServerForm(acServerList, form.getServId());
				if(acServer != null){
					form.setServName(acServer.getServName());
					form.setServIp(acServer.getServIp());
					form.setServPort(acServer.getServPort());
//					form.setAppUrl(OrgUtils.calcAppUrl(acServer, form.getAppCode()));
				}
				acApplicationMap.put(form.getAppId(), form);
			}
			
			return acApplicationMap;
		}else
			return null;
	}
	
	/**
	 * 将replacementList中的对象替换originalList中的相同主键的对象
	 * @param originalList
	 * @param replacementList
	 */
	public static void replaceAcServerFormList(List<AcServerForm> originalList, List<AcServerForm> replacementList){
		if(replacementList != null && replacementList.size() > 0){
			for(AcServerForm form : replacementList){
				//如果存在则先删除
				for(int i=0; i<originalList.size(); i++){
					if(form.getServId().longValue() == originalList.get(i).getServId().longValue()){
						originalList.remove(i);
					}
				}
				//重新同步更新
				originalList.add(form);
			}
		}
	}
	
	public static String getFuncNo(String entityName, Long appId, Long entityId){
		String result = "";
		if(entityName != null && entityName.trim().length() > 0 && entityId != null){
			result = entityName + MultiEntityTreeNode.SPLIT_CHAR + appId + MultiEntityTreeNode.SPLIT_CHAR + entityId;
		}
		return result;
	}
	
	/**
	 * 如果用户设置了funcNo属性，则保存新的应用ID和功能ID
	 * @param funcNo
	 * @param entity
	 */
	public static void calcAppIdAndFuncId(String funcNo, AcMenu entity){
		// 如果用户设置了funcNo属性，则保存新的功能ID
		if(funcNo != null && funcNo.trim().length() > 0){
			String[] arr = funcNo.split(MultiEntityTreeNode.SPLIT_CHAR);
			if(arr != null && arr.length == 3){
				entity.setAppId(Long.valueOf(arr[1]));
				entity.setFuncId(Long.valueOf(arr[2]));
			}
		}
	}
	
	/**
	 * 获取数据字典的ITEM名称
	 * @param typeCode
	 * @param itemCode
	 * @param sysCodeList
	 * @return
	 */
	public static String getItemName(String itemCode, List<SysCodeForm> sysCodeList){
		if(itemCode != null && itemCode.trim().length() > 0
				&& sysCodeList != null && sysCodeList.size() > 0){
			
			for(SysCodeForm sysCode : sysCodeList)
				if(itemCode.equals(sysCode.getItemCode()))
					return sysCode.getItemName();
				
		}
		
		return "";
	}
	
	public static String convertToString(Long[] array, String sep){
		StringBuffer sb = new StringBuffer();
		
		if(array != null && array.length > 0){
			for(int i=0; i<array.length; i++){
				sb.append(sep);
				sb.append(array[i]);	
			}
		}
		sb.append(sep);
		return sb.toString();
	}
	
	
	public static List<Long> convertArrayToList(Long...orgIds){
		List<Long>  list= null;
		if(orgIds != null && orgIds.length>0){
			list = new ArrayList<Long>();
			for(Long orgId:orgIds)
				list.add(orgId);
		}
		return list;
		
	}

	public static Long getTopOrgId(OmOrgForm org){
		if(org != null && org.getOrgSeq() != null)
			return Long.parseLong(org.getOrgSeq().split("\\.")[1]);
		else
			return null;
	}
	
	public static Long[] getTopOrgIds(List<OmOrgForm> authedOrgs){
		if (authedOrgs != null && authedOrgs.size() > 0) {
			Set<Long> topOrgIdSet = new HashSet();
			
			for(OmOrgForm org : authedOrgs) {
				topOrgIdSet.add(getTopOrgId(org));
			}

			return CollectionUtils.toLongArray(topOrgIdSet);
		}
		
		return null;
	}
	
	/**
	 * 根据指定机构序列获取所有关联的机构ID
	 * @param orgs
	 * @return
	 */
	public static Long[] getAllRelatedOrgIdsBySeq(List<OmOrgForm> orgs){
		if (orgs != null && orgs.size() > 0) {
			Set<Long> orgIdSet = new HashSet();
			
			for(OmOrgForm org : orgs) {
				orgIdSet.addAll(CollectionUtils.toLongList(splitSeqToArray(org.getOrgSeq())));
			}

			return CollectionUtils.toLongArray(orgIdSet);
		}
		
		return null;
	}
	
	public static void setOrgCheckedProp(List<OmOrgForm> forms, boolean checked){
		if(forms != null && forms.size() > 0){
			for(OmOrgForm form : forms){
				form.setChecked(checked);
			}
		}
	}
	
	/**
	 * 如果授权管理机构列表中包含顶级机构，则设置顶级机构对象Checked属性为true，表示用户可操作该机构树节点
	 * @param topOrgs 顶级机构列表
	 * @param authedOrgs 授权管理机构列表
	 */
	public static void setTopOrgCheckedProp(List<OmOrgForm> topOrgs, List<OmOrgForm> authedOrgs){
		if(authedOrgs != null && authedOrgs.size() > 0
				&& topOrgs != null && topOrgs.size() > 0){
			
			for(OmOrgForm topOrg : topOrgs){
				if(containOrg(topOrg, authedOrgs))
					topOrg.setChecked(true);
				else
					topOrg.setChecked(false);
			}
		}
	}
	
	public static boolean containOrg(OmOrgForm org, List<OmOrgForm> orgs){
		if(orgs != null && orgs.size() > 0 && org != null){
			for(OmOrgForm temp : orgs){
				if(temp.getOrgId().longValue() == org.getOrgId().longValue())
					return true;
			}
		}
		return false;
	}
	
	/**
	 * 获取带是否启用标志的菜单名称
	 */
	public static String getMenuNameWithActiveFlag(AcMenu entity){
		return (entity.getIsActive().equals(OrgConstant.ONE) ? "<font color='green' size='2'>√ </font>" : "<font color='red' size='2'>× </font>") + entity.getMenuName();
	}
	
	/**
	 * 获取模块Form对象列表的模块ID集合
	 * @param forms
	 * @return
	 */
	public static Long[] getModuleIds(List<AcModuleForm> forms){
		if(forms != null && forms.size() > 0){
			Long[] moduleIds = new Long[forms.size()];
			for(int i = 0; i < moduleIds.length; i++){
				moduleIds[i] = forms.get(i).getModuleId();
			}
			return moduleIds;
		}else
			return null;
	}
	
	/**
	 * 根据功能forms获取其对应的功能id数组
	 * @param funcs 功能form列表
	 * @return 功能id数组
	 */
	public static Long[] getFuncIds(List<AcFunctionForm> funcs){
		if(funcs != null && funcs.size() > 0 ){
			Long[] funcIds = new Long[funcs.size()];
			for(int i = 0; i < funcs.size(); i++){
				funcIds[i] = funcs.get(i).getFuncId();
			}
			return funcIds;
		}else
			return null;
	}
	
	/**
	 * 根据功能id求出其对应的菜单名称
	 * @param funcId 功能id
	 * @param menus 菜单列表
	 * @return 菜单名称
	 */
	public static List<String> getMenuNamesByFuncId(Long funcId, List<AcMenuForm> menus){
		
		if(funcId != null && menus != null && menus.size() > 0){
			List<String> menuNameList = new ArrayList();
			for(int i = 0; i < menus.size(); i++){
				AcMenuForm acMenu = menus.get(i);
				
				if(acMenu.getFuncId() != null && acMenu.getFuncId().longValue() == funcId.longValue())
					menuNameList.add(acMenu.getMenuName());
			}
			return menuNameList;
		}
		return null;
	}
	
	/**
	 * 将菜单名称列表转换为字符串形式
	 * @param menuNameList
	 * @return
	 */
	public static String convertToMenuNamesStr(List<String> menuNameList){
		String menuNames = "";
		if(menuNameList != null && menuNameList.size() > 0){
			menuNames += "<font color='#555555'>（菜单：";
			for(int i = 0; i < menuNameList.size(); i++){
				menuNames += menuNameList.get(i);
				if(i < menuNameList.size() - 1){
					menuNames += OrgConstant.STRING_SEPARATOR_D;
				}
			}
			menuNames += "）</font>";
		}
		return menuNames;
	}
	
	/**
	 * 根据机构id查询机构名称
	 * @param orgId 机构id
	 * @param list	机构列表
	 * @return	
	 */
	public static String getOrgName(Long orgId, List<OmOrgForm> list){
		if(orgId != null && list != null && list.size() > 0){
			for(OmOrgForm form:list){
				if(orgId.longValue() == form.getOrgId().longValue())
					return form.getOrgName();
			}
		}
		return "";
	}
	
	/**
	 * 根据工作组id查询工作组名称
	 * @param groupId 工作组id
	 * @param list	工作组列表
	 * @return	
	 */
	public static String getGroupName(Long groupId, List<OmGroupForm> list){
		if(groupId != null && list != null && list.size() > 0){
			for(OmGroupForm form:list){
				if(groupId.longValue() == form.getGroupId().longValue())
					return form.getGroupName();
			}
		}
		return "";
	}
	
	/**
	 * 根据对象seq分析得出对象的直属父对象的主键id
	 * @param objSeq 对象的seq
	 * @return
	 */
	public static Long getDirectParentObjId(String objSeq){
		String[] relatedOrgIdStr = objSeq.split("\\.");
		//非顶级对象
		if(relatedOrgIdStr.length >= 3){
			return Long.valueOf(relatedOrgIdStr[relatedOrgIdStr.length - 2]);
		}
		return null;
	}
	
	/**
	 * 根据对象seq分析得出对象的间接父对象的主键id列表
	 * @param objSeq
	 * @return
	 */
	public static List<Long> getIndirectParentObjIds(String objSeq){
		List<Long> orgIds = null;
		String[] relatedOrgIdStr = objSeq.split("\\.");
		
		//是否含有间接父对象
		if(relatedOrgIdStr.length >= 4){
			orgIds = new ArrayList<Long>();
			for(int i = relatedOrgIdStr.length - 3; i >= 1; i--){
				orgIds.add(Long.valueOf(relatedOrgIdStr[i]));
			}
		}
		return orgIds;
	}
	
	/**
	 * 根据对象seq分析得出对象的所有父对象（包括直接的和间接的）的主键id列表
	 * @param objSeq
	 * @return
	 */
	public static List<Long> getAllParentObjIds(String objSeq){
		List<Long> orgIds = null;
		String[] relatedOrgIdStr = objSeq.split("\\.");
		
		//是否含有间接父对象
		if(relatedOrgIdStr.length >= 3){
			orgIds = new ArrayList<Long>();
			for(int i = relatedOrgIdStr.length - 2; i >= 1; i--){
				orgIds.add(Long.valueOf(relatedOrgIdStr[i]));
			}
		}
		return orgIds;
	}
	
	/**
	 * 计算对象的seq
	 * @param objId 对象的主键
	 * @param parentObjId 父对象的主键
	 * @param parentSeq 父对象的seq
	 * @return
	 */
	public static String calSeqOfObj(Long objId, Long parentObjId, String parentObjSeq){
		//对象为顶级对象
		if(parentObjId == null){
			return "." + objId + ".";
		}
		return parentObjSeq + objId + ".";
	}
	
	/**
	 * 将用户对象数组转化为用户主键数组
	 * @param forms用户对象数组
	 * @return userIds 用户主键数组
	 * */
	public static Long[] convertUserFormsToIds(OmUserForm[] forms){
		if(forms != null && forms.length > 0){
			Long[] userIds = new Long[forms.length];
			for(int i=0; i<forms.length; i++){
				userIds[i] = forms[i].getUserId();
			}
			return userIds;
		}
		return null;
	}
	
	/**
	 * 将岗位form对象数组转化为岗位主键数组
	 * @param forms 岗位form对象数组
	 * @return 岗位主键数组
	 */
	public static Long[] convertPosFormsToIds(OmPositionForm[] forms){
		if(forms != null && forms.length != 0){
			Long[] posIds = new Long[forms.length];
			for (int i = 0; i < forms.length; i++) {
				posIds[i] = forms[i].getPosId();
			}
			return posIds;
		}
		
		return null;
	}
	
	/**
	 * 获取字符串最前面的指定字符数的子串
	 * @param str
	 * @param charCount
	 * @return
	 */
	public static String getPrefixString(String str, int charCount){
		String result = null;
		
		if(str != null){
			int len = str.length();
			
			if(charCount >= len)
				return str;
			else
				return str.substring(0, charCount) + "..";
		}
		
		return result;
	}
	
	/**
	 * 获取图片正确路径
	 * @param imgPath
	 * @param useFullImgPath
	 * @return
	 */
	public static String getImgPath(String imgPath, boolean useFullImgPath){
		if(imgPath != null && imgPath.trim().length() > 0){
			if(useFullImgPath)
				return Struts2Utils.getRequest().getContextPath() + imgPath;
			else
				return imgPath;
		}else
			return null;
	}
	
	/**
	 * 对于存在本地应用标识的要转换为本地应用名称
	 * @param menuUrl
	 * @param replaceLocalAppFlag
	 * @return
	 */
	public static String getMenuUrl(String menuUrl, boolean replaceLocalAppFlag){
		if(menuUrl != null && menuUrl.trim().length() > 0){
			if(replaceLocalAppFlag)
				return menuUrl.replaceFirst(OrgConstant.LOCAL_APP_FLAG_FOR_REPLACE, 
						Struts2Utils.getRequest().getContextPath() + "/");
			else
				return menuUrl;
		}else
			return null;
	}
	
	public static List<AcApplicationForm> convertToAcApplicationList(Map<Long, AcApplicationForm> map, boolean onlyLoadActiveApp){
		if(map != null && map.size() > 0){
			List<AcApplicationForm> result = new ArrayList();
			
			Iterator<Map.Entry<Long, AcApplicationForm>> it = map.entrySet().iterator();
			while(it.hasNext()){
				AcApplicationForm form = it.next().getValue();
				
				if(onlyLoadActiveApp && form.getIsActive().equals(OrgConstant.ONE))
					result.add(form);
				else
					result.add(form);
			}	
			
			return result;
		}
		return null;
	}
	
	/**
	 * 获取ExtTreeNode对象列表中菜单层次最小值
	 * @param list ExtTreeNode对象列表
	 * @return
	 */
	public static int getMinNodeLevel(List<MenuLinkTreeNode> list){
		int result = 99;
		
		if(list != null && list.size() > 0){
			for(MenuLinkTreeNode form : list){
				if(form.getLevel().intValue() < result)
					result = form.getLevel().intValue();
			}
		}
		
		return result;
	}
	
	/**
	 * 判断ExtTreeNode对象是否在指定列表中存在
	 * @param node ExtTreeNode对象
	 * @param list 要比较的ExtTreeNode对象列表
	 * @return
	 */
	public static boolean exist(MenuLinkTreeNode node, List<MenuLinkTreeNode> list){
		if(list != null && list.size() > 0){
			for(MenuLinkTreeNode temp : list){
				if(temp.getId().equals(node.getId()))
					return true;
			}
		}
		return false;
	}
	
	public static BaseTreeNode getNoPrivTipTreeNode(){
		BaseTreeNode tip = new BaseTreeNode();
		tip.setText("提示：您没有选择应用功能的权限！");
		tip.setLeaf(true);
		return tip;
	}
	
	public static String convertToBooleanStr(String str){
		if(str != null){
			if(str.equals(OrgConstant.ONE))
				return "是";
			else if(str.equals(OrgConstant.ZERO))
				return "否";
			else
				return str;
		}else
			return null;
	}
	
	public static void main(String[] args) {
		
		System.out.print("/${ctx}/test/test3".replaceFirst("/\\$\\{ctx\\}/", "flag"));
	}
}
